using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;
using RefreshModule;

namespace AnadoluBazaar
{
    [Refresh()]
    public partial class AnadoluBazaar : System.Web.UI.MasterPage
    {
        DataTable row;
        SqlDataAdapter ad2;
        private double runningTotal = 0;
        protected void Page_Load(object sender, EventArgs e)
        {
            Session["usertype"] = "";
            if (RefreshModule.RefreshHelper.IsPageRefreshed) { }
            Session["bugun"] = DateTime.Now.ToString();
            SqlDataSource1.SelectCommand = "SELECT TOP (4) products.productid, products.name, products.price, productimage.productimage FROM products INNER JOIN productimage ON products.imageid = productimage.productimageid WHERE (products.deadline > '" + DateTime.Now.ToString("MM.dd.yyyy") + "') ORDER BY products.productid DESC";
        
        }

        private void CalcTotal(string _price)
        {
            try
            {
                runningTotal += Double.Parse(_price);
            }
            catch (Exception ex)
            {
                HttpContext.Current.Response.Write(ex.ToString());
            }
        }
        protected void Login1_Authenticate(object sender, AuthenticateEventArgs e)
        {
            string query = "SELECT [enduserid], [user_name], [password],[endusertypeid] FROM [users] WHERE (([user_name] = N'" + Login1.UserName + "') and issubscribed=1)";
            SqlConnection con2 = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["shoppingConnectionString"].ToString());
            SqlCommand cmd2 = new SqlCommand(query, con2);
            SqlDataAdapter ad2 = new SqlDataAdapter(cmd2);
            DataTable dt2 = new DataTable();
            ad2.Fill(dt2);
            foreach (DataRow dr in dt2.Rows)
            {
                if (dr["password"].ToString() == Login1.Password)
                {
                    Session["us"] = "";
                    //Request.QueryString["CatID"] = "";
                    Session["username"] = Login1.UserName;
                    Session["userid"] = dr["enduserid"].ToString();
                    Session["password"] = dr["password"].ToString();
                    Session["usertype"] = dr["endusertypeid"].ToString();
                    Session.Timeout = 100000;
                    string srr = Session["usertype"].ToString();
                    Response.Redirect("BasariliGiris.aspx");
                }
            }
        }

        protected void ImageButton1_Click(object sender, ImageClickEventArgs e)
        {
            
        }

        protected void DataList1_ItemCommand(object source, DataListCommandEventArgs e)
        {
            
            if (e.CommandName == "sepetegonder")
            {
               
                string query = "SELECT name,price,productid FROM products WHERE productid='" + (string)e.CommandArgument + "'";
                SqlConnection con2 = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["shoppingConnectionString"].ToString());
                SqlCommand cmd2 = new SqlCommand(query, con2);
                ad2 = new SqlDataAdapter(cmd2);
                row= (DataTable)Session["row"];
             
                   if (Session["row"] == null)
                   {
                       row = new DataTable();
                       ad2.Fill(row);
                       
                       
                   }
                   else
                   { 
                       if (!RefreshHelper.IsPageRefreshed)
                       {
                           DataTable row2 = new DataTable();
                           row2 = (DataTable)Session["row"];
                           DataRow dr2 = row.NewRow();
                           foreach (DataRow drr in row2.Rows)
                           {
                               dr2 = drr;
                           }

                           ad2.Fill(row);

                       } 
                   }
                  
              
                
                //row.Rows.Add(dr);

             
                GridView1.DataSource = row;
                GridView1.DataBind();
                Session["row"] = row;
             

              //  DataTable dtProducts = new DataTable();
              //  DataColumn productColumn = new DataColumn();

              //  productColumn = new DataColumn();
              //  productColumn.DataType = System.Type.GetType("System.String");
              //  productColumn.ColumnName = "nam";
              //  dtProducts.Columns.Add(row.Columns[0]);

              //  productColumn = new DataColumn();
              //  productColumn.DataType = System.Type.GetType("System.Int32");
              //  productColumn.ColumnName = "pric";
              //  dtProducts.Columns.Add(row.Columns[1]);

              //  DataRow aProduct = dtProducts.NewRow();
            
              //  dtProducts.Rows.Add(aProduct);
              //  GridView1.DataSource = dtProducts;
              //  GridView1.DataBind();
              

          

            }
            if (e.CommandName == "detay")
            {
                Session["idproduct"] = (string)e.CommandArgument;
                Response.Redirect("UrunDetay.aspx");
            }


        }

        protected void LinkButton1_Click(object sender, EventArgs e)
        {

          
            
        }

        protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {
            //int x = (int)GridView1.DataKeys[e.RowIndex].Value;

            //DataTable row = (DataTable)Session["row"];

            //foreach (DataRow k in row.Rows)
            //{
            //    if ((k["productid"].ToString() == x.ToString())) { k.Delete(); break; }

            //}

            row = (DataTable)Session["row"];
            row.Rows[e.RowIndex].Delete();
            row.AcceptChanges();
            Session["row"] = row;
            
            GridView1.DataSource = row;
            GridView1.DataBind();
            
            
        }

        protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                CalcTotal(e.Row.Cells[2].Text);
            }
            else if (e.Row.RowType == DataControlRowType.Footer)
            {
                e.Row.Cells[0].Text = "Toplam";
                e.Row.Cells[1].Text = runningTotal.ToString();
                e.Row.Cells[2].Text = "YTL";
            }
        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            
            
        }

        protected void Button1_Click1(object sender, EventArgs e)
        {

        
       
        }

        protected void Button1_Click2(object sender, EventArgs e)
        {
          string paypal="https://www.paypal.com/cgi-bin/webscr?cmd=_xclick&business=hayros@gmail.com&item_name=Toplam&amount="+ GridView1.FooterRow.Cells[1].Text+"&no_shipping=2&no_note=1&currency_code=USD&lc=TR&bn=PP-BuyNowBF&charset=UTF-8";
          Response.Redirect(paypal);
          
            
        }

      

      
        


        
    }
}
